function ctrlInitArrowBindCtrl(context, selector, data){
	var context = $(selector,context);
	
	var table = $(".ctrl-bind-table",context);
	var list = $(".ctrl-bind-conn-list", context);
	var suggest = $('[name="suggest"]',context);
	var $dialogTrigger = $('.ctrl-bind-table-trigger',context);
	var simple = $(".ctrl-bind-simple", context);
	var hidden = $(".ctrl-bind-hidden", context);
	
	$(".bind-trigger-cell",context).width($(".bind-trigger-cell",context).children().length * 15 );
	
	// wymagane, gdy nie wywoła się change
	/* TO bardzo dużo psuło
	 * 
	 */
	if(data.mode == "filter"){
		suggest.blur(function() {
			hidden.val($(this).val());
		});
	}
	
	suggest.focus( function() { 
		if( suggest.val() == data.hint ) {
			suggest.removeClass( "ctrl-bind-suggest-empty" );
			suggest.val("");
		}
	} ).blur( function(e){
		if( suggest.val() == "" ) {
			suggest.addClass( "ctrl-bind-suggest-empty" );
			if( data.hint != false ) suggest.val( data.hint );
		}
	});
	
	
	var addElement = function(elId, html){
		//usuwanie scieżki z elId (wymagane przez IE 7)
		if( typeof elId == 'string' ) {
			elId = elId.split('/');
			elId = elId[elId.length-1];
		}
		var item = $( '<div class="ctrl-bind-item"><a href="'+elId+'" class="ctrl-bind-delete-item">&nbsp;</a></div>' ).append(html);
		if(data.mode == "advanced"){
			list.html(item);
			hidden.val(elId);
		}else if(data.mode == "multiAdvanced"){
			list.append(item);
			updateField( context );
		}else if(data.mode === "search"){
			hidden.val(elId);
			suggest.val(html.replace(/<\/?[^>]+(>|$)/g, "") );
		} else if(data.mode === "filter"){
			var tmp = '';
			term = suggest.val().split(',');
			for(var i = 0; i < term.length - 1; i++)
				tmp += term[i] + ',';
			
			if(tmp === ',')
				tmp = '';
			
			hidden.val(tmp+html.replace(/<\/?[^>]+(>|$)/g, ""));
			suggest.val(tmp+html.replace(/<\/?[^>]+(>|$)/g, ""));
			
		}else if(data.mode === "simple"){
			hidden.val(elId);
			list.show();
			list.html(item).appendTo(simple).click(function(e){
				list.hide();
				suggest.parent().css("display", "");
				
				suggest.show().blur( function(e){
					if(hidden.val() != ""){
						setTimeout(function(){
							list.show();
							suggest.hide();
						}, 300);
					}
					return true;
				}).focus();
			});
			suggest.hide();
			//chrome hack
			suggest.parent().hide();
			$('.ctrl-bind-title',context).hide();
			
		}
		
		if( data.selectedFn )
			try
				{window[data.selectedFn](elId, html);}
			catch(e){
				alert('Bug w selectedFn');
			}
	}	
	
	
	if( data.action ){
		$(".ctrl-bind-action a", context).click( function() { Page.reloadHandler= function() { 
			//window.parent.Page.lastId;
			$.post( data.source, {
				id: window.parent.Page.lastId,
				template: data.template
			}, 
			function(result){ 
				addElement( window.parent.Page.lastId, result[0].label  ) ;
			}
		);
			
			
			

			
		} ; } );
	}
	
	
	//var tWindow = (self == window.top)?self:window.top;
	
	if(data.mode === "simple"  && hidden.val() != ""){
		list.show();
		list.appendTo(simple).click(function(e){
			list.hide();
			
			suggest.parent().css("display", "");
			suggest.show().blur( function(e){
				
				if(hidden.val() != ""){
					setTimeout(function(){
						list.show();
						suggest.hide();
						//chrome hack
						suggest.parent().hide();
					}, 300);
				}
				return true;
			}).focus();
		});
		suggest.hide();
		$('.ctrl-bind-title',context).hide();
	}
	
	
	var updateField = function(context){
		var str = "";
		list.find("a").each(function(){
			str+=$(this).attr("href")+";";
		});
		hidden.val(str);
	}
	
	
	
	var offset = $dialogTrigger.offset();
	if (offset)
		offset.left+= $dialogTrigger.width()+5;
	
	$dialogTrigger.click( function(e){
		
		
		var x = e.pageX;
		var y = e.pageY;
		
		/*if( data.action ){
			$(".ctrl-bind-action a", context).click();
			return false;
		}*/
		
		CtrlAjax.changeStateVars( table.find('input'), {"initiated":"1"}, {}, function(){
		
			
			var title = $(".ctrl-table-title", table).text();
			window.top.$('#ctrl-tmp-bind-table').remove();
			
			var top_body = window.top.$('body');
			var tmpContainer = $('<div><div id="ctrl-tmp-bind-table"></div></div>');
			top_body.append(tmpContainer.html());
			tmpContainer = top_body.find('#ctrl-tmp-bind-table');
			tmpContainer.append(table.html());
			
			//var tmpContainer = $('<div id="ctrl-tmp-bind-table"></div>').appendTo(window.top.$('body')).append(table.html());
			
			var obj = window.top.$(tmpContainer).dialog({ autoOpen: true,  title: title, position:[x,y] , modal: true  });
		
			tmpContainer.delegate(".ctrl-bind-table-use-close", 'click', function(){
				addElement($(this).attr("href"), $(this).prev().html());
				window.top.$(tmpContainer).dialog('close');
				return false;
			});
			
			tmpContainer.delegate(".ctrl-bind-table-use", 'click', function(){
				addElement($(this).attr("href"), $(this).parent().prev().find('div').html());
				return false;
			});
			
			window.top.$(tmpContainer).delegate(".ctrl-table-quick-search input", 'keypress', function(e){
				if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
					window.top.$(this).next().click();
					return false;
				} else {
					return true;
				}
		    });
			
			window.top.$(tmpContainer).delegate(".ctrl-table-quick-search textarea", 'keypress', function(e){
				if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
					window.top.$(this).next().click();
					return false;
				} else {
					return true;
				}
		    });
			
			window.top.$(tmpContainer).delegate(".ctrl-table-quick-search input[type='button']", 'click', function(event){
				var input = window.top.$(this).prev();
				
				window.top.CtrlAjax.changeStateVars(input, {search: input.val()},{}, function(context){
					$(".ctrl-table-quick-search input",context).focus();
					
				});
				
			});
			
			
			var cssWidth = tmpContainer.find('table').css('width');
			
			if(cssWidth != 'auto'){
				width = parseInt(cssWidth.replace('px', '')) ;
				if( width < 500 ) width = 500;
				window.top.$(tmpContainer).dialog("option", 'width', width);
				window.top.$(tmpContainer).dialog("option", 'position', [x,y] );
			}else{
				var width = window.top.$(tmpContainer).dialog( "option", "width");
				
				if(width< 500 ){
					window.top.$(tmpContainer).dialog("option", 'width', 500);
					window.top.$(tmpContainer).dialog("option", 'position', [x,y] );
				}
			}
		
		
		});
		e.stopPropagation();
	});
	
	table.click(function(e){
	    e.stopPropagation();
	});
	

	
	list.find('a').live('click', function(){
		
		
		if( data.unBindFn )
			try { window[data.unBindFn]($(this).attr("href"));}
			catch(e){
				alert('Bug w selectedFn');
			}
		
		$(this).parent().remove();
		updateField( context );
		
		suggest.val("").blur();
		
		
		/*suggest.show();
		suggest.parent().css("diplay", '');
		alert("aa");*/
	
		return false;
	});
	
	/*list.sortable({
		 stop: function(event, ui) { updateField( context ); }
	});*/
	
	

	$(function(){
	
	suggest.autocomplete({
		source: function(request, response) {
			
			term = request.term.split(',');
			term = term[(term.length - 1)];
			
			$.post( data.source, {
					search: term,
					criteria: $(".ctrl-bind-criteria", context).html(),
					template: data.template
				}, 
				function(result){
					//alert(result);
					//ArrowResponseReader.read(result);
					//alert(result);
					response( result );
				}
			);
		},
		focus: function(event, ui) {
			if(data.mode === "filter")
				return false;
		},
		
    	minLength: 1,
		select: function(event, ui) {
			if(ui.item && ui.item.id){
				addElement(ui.item.id, ui.item.label);
				if(data.mode === "search" || data.mode === "filter"){
					ui.item.value = ui.item.label.replace(/<\/?[^>]+(>|$)/g, "");
				}
			}
			return false;
		},
		
		change: function(event, ui) {
			//$(event.target).val("");
			// potrzebne w filtrach
			
			if(data.mode === "filter")
				hidden.val($(this).val());
			return false;
		}
		
		

		
	})
	
	var autocomplete = suggest.data( "autocomplete" )
	if( autocomplete ) {
		autocomplete._renderItem = function( ul, item ) {
			return $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( "<a>" + item.label +"</a>" )
				.appendTo( ul );
		} ;
	}
	
	})

		
	
}